﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace PasswordProvider.ViewModel.Framework
{
    /// <summary>
    /// ListSelectViewModel: Encapsulates the data and business rules for editing a value that is 
    /// selected from a list of values. Inherits from ListEditViewModel and adds properties SelectedItem
    /// and SelectedItems.
    /// </summary>
    public class ListSelectViewModel<T> : ListEditViewModel<T>
    {
        #region Events

        internal event EventHandler<ValueChangedEventArgs<T>> SelectedItemChanged;
        internal event EventHandler SelectedItemsChanged;

        #endregion

        #region Construction

        internal ListSelectViewModel()
            : base() 
        { }

        internal ListSelectViewModel(string displayName, string toolTip, IEnumerable<T> itemsSource, T selectedItem)
            : base(displayName, toolTip, itemsSource)
        {
            _selectedItem = selectedItem;
        }
        internal ListSelectViewModel(string displayName, string toolTip, IEnumerable<T> itemsSource, IEnumerable<T> selectedItems)
            : base(displayName, toolTip, itemsSource)
        {
            if(selectedItems != null)
                SelectedItems = new ObservableCollection<T>(selectedItems);
        }

        #endregion // Construction

        #region Public Properties

        /// <summary>
        /// The selected item.
        /// </summary>
        public T SelectedItem
        {
            get { return _selectedItem; }
            set 
            {
                T origValue = _selectedItem;
                if(SetPropertyValue(value, ref _selectedItem, "SelectedItem"))
                {
                    if (SelectedItemChanged != null)
                    {
                        ValueChangedEventArgs<T> e = new ValueChangedEventArgs<T>(origValue, _selectedItem);
                        SelectedItemChanged(this, e);
                        if (e.Cancel)
                            _selectedItem = origValue;                       
                    }
                }
            }
        } private T _selectedItem;

        /// <summary>
        /// The selected items. For MultiSelect combo and list boxes.
        /// </summary>
        public ObservableCollection<T> SelectedItems
        {
            get 
            {
                if (_selectedItems == null)
                {
                    SelectedItems = new ObservableCollection<T>();                
                }
                return _selectedItems; 
            }
            private set
            {
                _selectedItems = value;
                _selectedItems.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(SelectedItems_CollectionChanged);                    
            }
        } private ObservableCollection<T> _selectedItems;

        void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            OnPropertyChanged("SelectedItems");
            if (SelectedItemsChanged != null)
            {                
                SelectedItemsChanged(this, new EventArgs());
            }        
        } 

        #endregion // Public Properties

        #region Private Methods

        #endregion
    }
}
